
global nu = 3
global rho = 0.738
global plist = "2"

**************************
* INSHEETING THE DATA 
**************************
foreach var in bc bp {
	foreach type in $plist {
*			local var = "bc"
*			local type = "5"
			
			*with informal
			insheet using "$est/Counterfactuals_1s_`var'_`type'A.csv", clear 
			gen inf = 1
			gen type = "`var'"
			gen dw = `type'
			tempfile `var'`type'1
			save ``var'`type'1', replace

			
			*rigid wages
			*insheet using "$dropbox/firmsize/Counterfactuals_1s_`var'_`type'C.csv", clear 
			*gen inf = 0
*			replace v9 = "0" if v9 == "NaN"
*			destring v9, replace
			*gen type = "`var'"
			*gen dw = `type'
			*tempfile `var'`type'0
			*save ``var'`type'0', replace
		
	}
}


*Baseline
insheet using "$est/Counterfactuals_1s_bc_1A.csv", clear 
gen inf = 1
gen type = "baseline"
gen dw = 1

foreach var in bc bp {
	foreach type in $plist {
		append using ``var'`type'1', force
	}
}


*renaming variables
rename v1 nic
rename v2 alpha 
rename v3 bc
rename v4 bp 
rename v5 p 
rename v6 cratio 
rename v7 w 
rename v8 dbc
rename v9 income
rename v10 TFP
rename v11 zi 
rename v12 zf
rename v13 zstari 
rename v14 zstarf
rename v15 li 
rename v16 lf
rename v17 yi
rename v18 yf
rename v19 ni
rename v20 nf
 
order nic type dw inf 
gsort nic -inf type dw

*generating some variables for analysis 
*replace zstari = . if inf == 0
 
gen penalty = .
foreach var in bc bp {
	gen temp = `var' if type == "baseline"
	egen tempmax = max(temp)
	replace penalty = `var'/tempmax - 1 if type == "`var'"
	drop temp*
}

*gen cratio = (penalty*bc/bp)^-$nu if type == "bc"
*replace cratio = (bc/(penalty*bp))^-$nu if type == "bp"
gen frac_contract = cratio/(1+cratio)
order penalty frac_contract, after(cratio)

gen y = yi + yf
gen l = li + lf
gen n = ni + nf
gen zbar_i = zi/ni
gen zbar_f = zf/nf
gen zbar_tot = (zi+zf)/n
gen frac_li = li/l
gen frac_yi = yi/y
gen frac_ni = ni/n
gen lbari = li/ni 
gen lbarf = lf/nf
gen relw = w/p 
gen wp = w*bp
gen tfp = (yi + yf)/(li + lf)^$rho 
 
*la def lc 1 "Baseline" 2 "10%" 3 "11%" 4 "12%" 5 "13%" 6 "14%" 
*la val dw lc 

*la def ladj 0 "Inflexible" 5 "5% change" 100 "Flexible"
*la val adjw ladj

*la def lyesno 0 "No" 1 "Yes"
*la val inf lyesno

gen frac_lf = 1 - frac_li
gen frac_nf = 1 - frac_ni

*Percentage change from the baseline
foreach var of varlist  p w wp relw y yi yf ///
						l li lf lbari lbarf ///
						n ni nf frac_lf frac_nf ///
						zi zf zbar_* {
	gen temp = `var' if dw == 1 & inf == 1
	egen temp2 = min(temp), by(nic)
	gen diff`var' = (`var')/temp2
	drop temp*
}

foreach var of varlist  zstar? {
	gen temp = zstari if dw == 1 & inf == 1
	egen temp2 = min(temp), by(nic)
	gen diff`var' = (`var')/temp2
	drop temp*
}


gen temp = bc/bp if dw == 1 & inf == 1
egen bcbp = max(temp), by(nic)
drop temp 

gen temp = frac_contract if dw == 1 & inf == 1
egen fc0 = max(temp), by(nic)
drop temp 

*foreach var of varlist zstari zstarf {
*	gen temp = zstari if dw == 1 & inf == 1
*	egen temp2 = min(temp), by(nic)
*	gen diff`var' = (`var')/temp2
*	drop temp*
*}

 
***********************
* SUMMARY STATISTICS 
***********************
global datavars = "frac* penalty diff*"
*drop if c == 5
gen lny = alpha*ln(y)
gen lnzi = alpha*ln(zi)
gen lnzf = alpha*ln(zf)
gen lntfp = alpha*ln(TFP)

collapse (first) income* w TFP (sum) lny lnzi lnzf lntfp n l (mean) $datavars zbar*, by(type dw inf)
gen Z_i = exp(lnzi) 
gen Z_f = exp(lnzf)
gen Z = exp(lntfp)

rename zbar* Zbar*
 
foreach var of varlist frac_contract frac_ni frac_li income* Z_* Zbar* Z TFP {
	gen temp = `var' if dw == 1 & inf == 1
	egen temp2 = min(temp)
	gen diff`var' = (`var')/temp2 if inf == 1
	drop temp*
}
replace penalty = 0 if type == "baseline"

gsort -inf type dw

la var penalty "$b_c$"
la var frac_contract "Frac. Contract workers"
la var frac_ni "Frac. Informal firms"
la var frac_li "Frac. Informal workers"
la var diffwp "Real wages"
la var diffzstari "Entry threshold"
la var diffzstarf "Formalization threshold"
la var diffTFP "TFP"
la var diffincome "Real Income (Welfare)"


mat T = J(9,3,.)

local i = 0
foreach var of varlist penalty difffrac_contract difffrac_ni difffrac_li diffzstari diffzstarf ///
			diffTFP diffrelw diffincome {
	local i = `i' + 1 

	qui summ `var' if type == "baseline"
	mat T[`i',1] = `r(mean)'
	qui summ `var' if type == "bc"
	mat T[`i',2] = `r(mean)'
	qui summ `var' if type == "bp"
	mat T[`i',3] = `r(mean)'
}
	mat T[1,2] = 1 + T[1,2]
	mat T[1,3] = 1 + T[1,3]

frmttable using "${output}/calibration/Table7.tex", statmat(T) sdec(2, 2, 2) ///
	ctitle(" " "Baseline", "Penalizing Contract Workers", "Subsidizing Payroll Workers") ///
	rtitle("Policy change"\"Frac. Contract Workers"\"Frac. Informal Firms"\"Frac. Informal Labor"\"Informal Sector"\"Formal Sector"\"TFP"\"Real Wages"\"Real Income (Welfare)") ///
	replace tex pretext(" ") spaceaft(1110101{0}) spaceht(3) fragment





